package com.atguigu.srb.sms.listener;

import com.atguigu.srb.mq.pojo.dto.SmsDTO;
import com.atguigu.srb.sms.service.SmsService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;


@Component
public class SmsListener {
    @Resource
    SmsService smsService;

    @RabbitListener(bindings =@QueueBinding(
            value = @Queue(value ="queue.sms.item",durable = "true"),
            key = {"routing.sms.item"},
            exchange = @Exchange(value = "exchange.topic.sms",durable = "true")
            ))
    public void a(Channel channel , Message message, SmsDTO smsDTO){

        System.out.println("消费消息");
        System.out.println(smsDTO);

        //手动确认消费消息
        try {
            smsService.sendMessage(smsDTO);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
